<?php

namespace app\api\controller;

use app\BaseController;
use app\common\service\around\JtkAroundService;
use app\common\service\around\JtkLocationService;
use app\common\service\order\CouponOrderService;
use app\model\CouponOrder;
use app\model\JtkGoods;
use app\model\JtkGoodsShop;
use app\model\JtkLocation;

/**
 * 周边游
 */
class Around extends BaseController
{
    protected $middleware = [
        \app\middleware\ApiChannel::class,
        \app\middleware\ApiUserAuthV2::class => ['except' => ['phoneInfo']],
    ];

    /**
     * 站点列表
     * @return \think\response\Json
     */
    public function getLocationList() {
        $result = JtkAroundService::getInstance()->getLocationListWord();
        return success('成功！', $result);
    }

    // 保存站点
    public function saveLocationList() {
        $result = JtkLocation::saveLocationData();
        return success('成功！', $result);
    }


    /**
     * 站点下的分类信息
     * @return \think\response\Json
     */
    public function productCategory() {
        $params = input('');
        $result = JtkAroundService::getInstance()->getProductCategory();
//        $result = JtkAroundService::getInstance()->getProductCategoryJtk($params);
        return success('成功！', $result);
    }

    public function productListJtk() {
        $params = input('');
        $result = JtkAroundService::getInstance()->productListJtk($params);
        return success('成功！', $result);
    }



    /**
     * 商品列表
     * @return \think\response\Json
     */
    public function productList() {
        $params = input('post.');
        $result = JtkAroundService::getInstance()->productList($params);
        if (isset($result['status']) && $result['status'] == false) {
            return fail($result['msg']);
        }
        return success('成功！', $result['data'] ?? []);
    }

    /**
     * 商品基本信息
     * @return \think\response\Json
     */
    public function productInfo() {
        $params = input('post.');
        $latitude = $params['latitude'] ?? '';
        $longitude = $params['longitude'] ?? '';
//        if ((empty($latitude) || empty($longitude))) {
//            return fail('经纬度参数不能为空');
//        }
        $result = JtkAroundService::getInstance()->productInfo($params);
        if ($result['status'] == false) {
            return fail($result['msg']);
        }
        return success('成功！', $result['data']);
    }

    /**
     * 商品图文详情--查询产品图文详情(文案)
     * @return \think\response\Json
     */
    public function productDetailV1() {
        $params = input('post.');
//        $params['productId'] = 950671;
        $result = JtkAroundService::getInstance()->productDetailV1($params);
        if ($result['status'] == false) {
            return fail($result['msg']);
        }
//        echo $result['data']['htmlContent']; die();
        return success('成功！', $result['data']);
    }

    /**
     * 商品图文详情--查询产品图文详情(文案)
     * 需要渠道对接方自行处理前端样式
     * @return \think\response\Json|void
     */
    public function productDetailV2() {
        $params = input('post.');
//        $params['productId'] = 950671;
        $result = JtkAroundService::getInstance()->productDetailV2($params);
        if ($result['status'] == false) {
            return fail($result['msg']);
        }
//        echo $result['data']['htmlContent']; die();
        return success('成功！', $result['data']);
    }

    public function createOrder() {
        $couponOrder = JtkAroundService::getInstance()->createTest();
        return success('成功！', $couponOrder);
    }

    /**
     * 申请退款
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function applyRefund() {
        $params = input('post.');
        $id = $params['id'] ?? '';
        if (empty($id)) {
            return fail('订单参数ID不能为空');
        }
        $couponOrder = CouponOrder::query()->where('id', $id)->find();
        if (empty($couponOrder)) {
            return fail('订单不存在');
        }
        $result = JtkAroundService::getInstance()->refundOrder($couponOrder);
        if ($result['status']) {
            return success($result['msg']);
        }
        return fail($result['msg']);
    }

    // 订单详情链接
    public function getDetailUrl() {
        $id = input('id');
        return env('app.api_url') . '/u/' . alphaID($id);
    }

}